﻿/* 
 * BtCommLib - the OpenWebNet .NET Library
 *
 * Copyright (C) 2011 BTicino S.p.A.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BTicino.BtCommLib.Communication
{
    public class BTCommErr
    {

        #region Data member
        private long    m_errCode;
        private string  m_errMsg;
        private string m_errArgs;
        private m_errorType m_errEnum;

        /// <summary>
        /// List of BtCommLib error
        /// </summary>
        public enum m_errorType
        {
            e_NoError          = 0,

            e_NotConnected     = 1001,
            e_AlreadyConnected = 1002,
            e_NotInRange       = 1003,

            e_UnableBuilOpenFrame = 2001,
            e_UnableSendFrame     = 2002,
            e_MissedOpenFrame     = 2003,
            e_ConnNotAvailable    = 2010
        }

        /// <summary>
        /// Translation from m_errorType to high level language
        /// </summary>
        private static Dictionary<m_errorType, String> m_errorList = new Dictionary<m_errorType, string>() { {m_errorType.e_NoError, "No Error"},
                                                                                                             {m_errorType.e_NotConnected, "Unable to connect the gateway"},
                                                                                                             {m_errorType.e_NotInRange, "Unable to connect the gateway, IP Not in range"},
                                                                                                             {m_errorType.e_AlreadyConnected, "Is Already connected"},
                                                                                                             {m_errorType.e_ConnNotAvailable, "Connection not available"}, 
                                                                                                             {m_errorType.e_UnableBuilOpenFrame, "Unable to build an Open Frame"},
                                                                                                             {m_errorType.e_UnableSendFrame, "Frame not send"},
                                                                                                             {m_errorType.e_MissedOpenFrame, "Missed Open Frame"}
                                                                                                           };
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor without parameter
        /// </summary>
        public BTCommErr()
        {
            m_errCode = 0;
            m_errMsg = string.Empty;
            ErrorEnum = m_errorType.e_NoError;
            ErrorArgs = string.Empty;
        }

        /// <summary>
        /// Constructor with error type as parameter
        /// </summary>
        /// <param name="theErrCode">Error Type, select from List of e_errorType</param>
        public BTCommErr(m_errorType theErrCode)
        {
            m_errCode = (long)theErrCode;
            m_errMsg = m_errorList[theErrCode];
            ErrorEnum = theErrCode;
            ErrorArgs = string.Empty;
        }

        /// <summary>
        /// Constructor with error type and error message as parameters
        /// </summary>
        /// <param name="theErrCode">Error Type, select from List of e_errorType</param>
        /// <param name="errorArgs">Error message, more information on the error</param>
        public BTCommErr(m_errorType theErrCode, string errorArgs)
        {
            m_errCode = (long)theErrCode;
            m_errMsg = m_errorList[theErrCode];
            ErrorEnum = theErrCode;
            ErrorArgs = errorArgs;
        }

        #endregion

        #region Public Variable Set / Get

        public long ErrCode
        {
            get { return(m_errCode); }
        }

        public string ErrMsg
        {
            get { return (m_errMsg); }
        }

        public bool isErr()  
        {
            if (m_errCode == 0)
                return false;
            else
                return true;
        }

        public m_errorType ErrorEnum
        {
            set { m_errEnum = value; }
            get { return (m_errEnum); }
        }

        public string ErrorArgs
        {
            set { m_errArgs = value; }
            get { return m_errArgs; }
        }

        #endregion

    }
}

